from django.db import models
from django.contrib.auth.models import User

from tinymce.models import HTMLField


class Category(models.Model):
    name = models.CharField(max_length=40)

    def __str__(self):
        return self.name


class Tag(models.Model):
    name = models.CharField(max_length=40)
    description = models.CharField(max_length=1000)

    def __str__(self):
        return self.name


class Article(models.Model):
    RECOMMENDED = 'R'
    APPROVED = 'A'
    TRANSLATED = 'T'
    STATUS = (
        (RECOMMENDED, 'Recommended'),
        (APPROVED, 'Approved'),
        (TRANSLATED, 'Translated'),
    )

    recommend_user = models.ForeignKey(User)
    category = models.ForeignKey(Category)
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    original_title  = models.CharField(max_length=200)
    original_link = models.URLField()
    title  = models.CharField(max_length=200)
    status = models.CharField(max_length=1, choices=STATUS, default=RECOMMENDED)
    recommend_date = models.DateTimeField(auto_now_add=True)
    translated_date = models.DateTimeField(null=True, blank=True)
    view_count = models.PositiveIntegerField(default=0)
    translate_rate = models.PositiveIntegerField(default=0)

    def update_view_count(self):
        self.view_count += 1
        self.save()

    def calculate_translate_rate(self):
        if self.status == Article.RECOMMENDED:
            return 0
        elif self.status == Article.TRANSLATED:
            return 100
        else:
            sections = self.section_set.all()
            trcount = uncount = 0
            for section in sections:
                if section.has_translated:
                    trcount += 1
                else:
                    uncount += 1
            return round(trcount / (trcount + uncount) * 100)

    def update_translate_rate(self):
        self.translate_rate = self.calculate_translate_rate()
        self.save()

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-recommend_date',)


class Section(models.Model):
    article = models.ForeignKey(Article)
    content = HTMLField()
    index = models.IntegerField(default=0)

    @property
    def has_translated(self):
        return True if self.translation_set.count() else False

    @property
    def best_translation(self):
        translations = self.translation_set.all()
        return translations.first()

    def __str__(self):
        return '[{0}] 第{1}段--{2}'.format(self.article.title, self.index, 
            '已翻译' if self.has_translated else '未翻译')


class Translation(models.Model):
    section = models.ForeignKey(Section)
    content = HTMLField()
    votes = models.IntegerField(default=0)
    translator = models.ForeignKey(User)
    update_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        if len(self.content) > 120:
            return '{0}...'.format(self.content[:120])
        else:
            return self.content

    class Meta:
        ordering = ('-votes',)
